const fs = require('fs');
const path = require('path');

function ttfToBase64(ttfFilePath) {
    return new Promise((resolve, reject) => {
        // 读取.ttf文件
        fs.readFile(ttfFilePath, (err, data) => {
            if (err) {
                reject(err);
                return;
            }

            // 将文件内容（Buffer）转换为Base64字符串
            const base64String = data.toString('base64');
            resolve(base64String);
        });
    });
}

// TTF 文件转 base64

const ttfFilePath = './public/fonts/SIMHEI.TTF';
ttfToBase64(ttfFilePath)
    .then(base64 => {

        // base64ToFile(base64, './SIMHEI')
        // console.log(path.parse(ttfFilePath))
        var p = path.parse(ttfFilePath);
        fs.writeFileSync(`${p.dir}/${p.name}.b64`, base64)
        console.log('TTF file as Base64:', base64.slice(0, 20));
    })
    .catch(error => {
        console.error('Error converting TTF to Base64:', error);
    });